import json

import requests
from concurrent.futures import ThreadPoolExecutor

# 定义请求的URL
url = 'http://localhost:5000/v1/chat/completions'

# 定义请求头
headers = {
    "Content-Type": "application/json"
}

# 定义要发送的数据
data = {
    "model": "custom-model",
    "messages": [
        {"role": "user", "content": "Hello!"},
        {"role": "assistant", "content": "Hi! How can I help?"},
        {"role": "user", "content": "What's the weather today?"}
    ]
}

# 将数据转换为JSON字符串
json_data = json.dumps(data)


# 定义发送请求的函数
def send_request():
    try:
        # 发送POST请求
        response = requests.post(url, headers=headers, data=json_data)

        # 打印响应内容
        print(f'Response status code: {response.status_code}')
        print(response.json())
    except Exception as e:
        print(f'Error occurred: {e}')

num = 10
# 使用ThreadPoolExecutor来并行发送请求
with ThreadPoolExecutor(max_workers=num) as executor:
    futures = [executor.submit(send_request) for _ in range(num)]

    # 等待所有线程完成
    for future in futures:
        future.result()
